package com.qdlc.p2p.biz.service.impl;

import java.util.List;

import javax.annotation.Resource;

import org.apache.log4j.Logger;
import org.springframework.stereotype.Repository;

import com.qdlc.p2p.biz.service.ExperienceService;
import com.qdlc.p2p.common.exception.UserException;
import com.qdlc.p2p.common.util.StringUtil;
import com.qdlc.p2p.dal.common.model.jpa.PageDataList;
import com.qdlc.p2p.dal.dao.ExperienceDao;
import com.qdlc.p2p.dal.dao.ExperienceRuleDao;
import com.qdlc.p2p.dal.dto.Experience;
import com.qdlc.p2p.dal.dto.User;
import com.qdlc.p2p.dal.model.ExperienceModel;

/**
 * 体验券service实现类
 *
 * @author xxx
 * @version 2.0
 * @date 2015年4月23日 下午8:02:57
 */
@Repository("experienceService")
public class ExperienceServiceImpl implements ExperienceService {

    private Logger logger = Logger.getLogger(ExperienceServiceImpl.class);

    @Resource
    private ExperienceDao experienceDao;

    @Resource
    private ExperienceRuleDao experienceRuleDao;

    @Override
    public PageDataList<ExperienceModel> findExperience(long userId) {
        ExperienceModel model = new ExperienceModel();
        model.setUserId(userId);
        model.setStatus((byte) 99);
        model.setPage(1);
        model.setRows(10);
        PageDataList<ExperienceModel> pageDataList = experienceDao.findUserList(model);
        return pageDataList;
    }

    @Override
    public PageDataList<ExperienceModel> findByModel(ExperienceModel model) {
        PageDataList<ExperienceModel> pageDateList = null;
        if (StringUtil.isNotBlank(model.getSearchName())) {
            //模糊查询条件
            pageDateList = experienceDao.findByUserName(model.getSearchName().trim(), model.getPage(), model.getRows());
        } else {
            //精确查询条件
            pageDateList = experienceDao.findByModel(model);
        }
        return pageDateList;
    }

    @Override
    public Experience save(Experience experience) throws Exception {

        // 更新体验券规则里的已发放名额
        int result = experienceRuleDao.updateQuotaYes(experience.getExperienceRuleId());
        if (result != 1) {
            logger.error("更新体验券已发放名额失败！名额已使用完，体验券发放失败!");
            throw new UserException("名额已使用完，体验券发放失败!");
        }
        // 保存体验券
        return experienceDao.save(experience);
    }

    @Override
    public Experience findById(long id) {
        return experienceDao.find(id);
    }

    @Override
    public int countUsableExperience(long userId) {
        return experienceDao.countUsableExperience(userId);
    }

    @Override
    public List<Experience> findUsableExperience(long userId) {
        return experienceDao.findUsableExperience(userId);
    }

    @Override
    public void update(Experience experience) {
        experienceDao.update(experience);
    }

    @Override
    public int updateStatus(long id, byte status, byte preStatus) {
        return experienceDao.updateStatus(id, status, preStatus);
    }

    @Override
    public double sumAmount(long[] ids, long user_id) {
        return experienceDao.sumAmount(ids, user_id);
    }

    @Override
    public Object[] getCapitalAndInterestByBorrowAndPeriod(long borrowId, int period) {
        return experienceDao.getCapitalAndInterestByBorrowAndPeriod(borrowId, period);
    }

    @Override
    public boolean checkExperience(long[] experienceIds, User user) {
        return experienceDao.checkExperience(experienceIds, user);
    }

    @Override
    public double[] getExperienceByYear(int status, String year) {
        double[] amounts = new double[12];
        String date;
        for (int i = 0; i < 12; i++) {
            date = year + (i > 8 ? "-" : "-0") + (i + 1);
            amounts[i] = experienceDao.getExperienceSumByStatus(status, date);
        }
        return amounts;
    }

    @Override
    public PageDataList<ExperienceModel> findUserList(ExperienceModel model) {
        return experienceDao.findUserList(model);
    }

    @Override
    public void experienceExpiredDispose() {
        experienceDao.eperienceExpiredDispose();
    }
}
